/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package giormi;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
 *
 * @author Ling
 */
public class F_updateproduct {
    private String p_status;
    private String p_barcode;
    private String p_name;
    private String p_packing;
    private String p_netweight;
    private int idproduct;

    public void setP_barcode(String p_barcode) {
        this.p_barcode = p_barcode;
    }

    public void setP_name(String p_name) {
        this.p_name = p_name;
    }

    public void setP_netweight(String p_netweight) {
        this.p_netweight = p_netweight;
    }

    public void setP_packing(String p_packing) {
        this.p_packing = p_packing;
    }

    public void setP_status(String p_status) {
        this.p_status = p_status;
    }
    
    public void set_idproduct(int idproduct) {
        this.idproduct = idproduct;
    }
    
    //GETTERS
    public String getP_barcode() {
        return this.p_barcode;
    }
    
    public String getP_name() {
        return this.p_name;
    }
    
    public String getP_netweight() {
        return this.p_netweight;
    }
    
    public String getP_packing() {
        return this.p_packing;
    }
    
    public String getP_status() {
        return this.p_status;
    }
    
    public int get_idproduct() {
        return this.idproduct;
    }

    private Connection conn;
    private String SQLStatement;
    private String URL;
    private Statement stmt;
    public ResultSet rset;

    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public void getproduct(int idproduct){
        this.idproduct = idproduct;
        connect ();
        try{
            SQLStatement = "SELECT * FROM product WHERE idproduct = " +idproduct;
            rset = stmt.executeQuery(SQLStatement);
            if(rset.next()){
                idproduct = rset.getInt(1);
                this.p_status = rset.getString(2);
                this.p_barcode = rset.getString(3);
                this.p_name= rset.getString(4);
                this.p_packing = rset.getString(5);
                this.p_netweight = rset.getString(6);
            }
            rset.close();
            disconnect();
            System.out.println("status"+p_status);
        }

        catch(Exception ex){
//            System.out.println(ex.getMessage());
//            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public String getpacking(int num) {
        String side = new String();
        int ctr = 0;
        System.out.println("packing:"+this.p_packing);
        if(num == 1){
            System.out.println(this.p_packing);
            for(ctr = 0; this.p_packing.charAt(ctr) != 'x'; ctr++) {
                side = side.concat(Character.toString(this.p_packing.charAt(ctr)));
            System.out.println("1:"+side);
        }
        }
        else if (num == 2) {
            System.out.println("GOES THROUGH SIDE?");
            for(ctr = 1; this.p_packing.charAt(ctr) != 'x'; ctr++){}
            ctr++;
            while (this.p_packing.charAt(ctr) < 'a') {
                side = side.concat(Character.toString(this.p_packing.charAt(ctr)));
                ctr++;
            }
            System.out.println("2:"+side);
        }
        else {
            int space=46;
            System.out.println(this.p_packing.length());
            for(ctr = 0; ctr < this.p_packing.length(); ctr++){
                System.out.println(ctr+" "+Character.toString(this.p_packing.charAt(ctr)));
                if(Character.toString(this.p_packing.charAt(ctr)).equals(" "))
                    space = ctr;

                if (ctr>space) {
                    side = side.concat(Character.toString(this.p_packing.charAt(ctr)));
                }
            }
            System.out.println("3:"+side);
        }
        return side;
    }
    
    public String getnetweight(int num) {
        String side = new String();
        int ctr = 0;
        System.out.println("netweight:"+p_netweight);
        if(p_netweight==null)
            return side;

        else {
            if (num == 1) {
                while (this.p_netweight.charAt(ctr) < 'a') {
                    side = side.concat(Character.toString(this.p_netweight.charAt(ctr)));
                    System.out.println(side);
                    ctr++;
                }
            }
            else {
                int space=46;
                System.out.println(this.p_netweight.length());
                for(ctr = 0; ctr < this.p_netweight.length(); ctr++){
                    System.out.println(ctr+" "+Character.toString(this.p_netweight.charAt(ctr)));
                    if(Character.toString(this.p_netweight.charAt(ctr)).equals(" "))
                        space = ctr;

                    if (ctr>space) {
                        side = side.concat(Character.toString(this.p_netweight.charAt(ctr)));
                    }
                }
                System.out.println("side2:"+side);
            }
        }
        return side;
    }
    
    
    public void updateproduct(){
        try{
           SQLStatement = "UPDATE product SET "
                   + "p_name = '" + p_name +
                   "',p_barcode = '" + p_barcode +
                   "',p_packing = '" + p_packing +
                   "',p_netweight = '" + p_netweight +
                   "',p_status = '" + p_status +
                   "' WHERE idproduct = " + idproduct;                   
            stmt.executeUpdate(SQLStatement);
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }
}
